/*
 * @Author: lujing
 * @Date: 2024-06-13 09:29:14
 * @LastEditTime: 2024-06-15 16:20:24
 * @FilePath: \workspace\fy\aiDigitalPerson\dipu-xjy-meta-digital-person\src\components\digital\MessageList.tsx
 */
import { Box, Typography } from '@mui/material';
import { random } from 'lodash';
import { useRecoilValue } from 'recoil';

import { IAction } from 'state/action';
import { INestedMessage, loadingState } from 'state/chat';
import { IMessageElement } from 'state/element';

import Message from './chat/message/message';

interface Props {
  messages: INestedMessage[];
  elements: IMessageElement[];
  actions: IAction[];
  indent: number;
  isRunning?: boolean;
  setRandom: any;
  random: any
}

export default function MessageList({
  messages,
  elements,
  actions,
  indent,
  isRunning,
  setRandom,
  random
}: Props) {
  const loading = useRecoilValue(loadingState);
  let previousAuthor = '';
  const filtered = messages.filter((m, i) => {
    const hasContent = !!m.content;
    const hasChildren = !!m.subMessages?.length;
    const isLast = i === messages.length - 1;
    const _isRunning =
      isRunning === undefined ? loading && isLast : isRunning && isLast;
    return hasContent || hasChildren || (!hasContent && _isRunning);
  });
  return (
  <>
    {filtered.map((m, i) => {
      const isLast = i === filtered.length - 1;
      const _isRunning =
        isRunning === undefined ? loading && isLast : isRunning && isLast;
      const showAvatar = m.author !== previousAuthor;
      const showBorder = false;
      previousAuthor = m.author;
      return (
        <Message
          message={m}
          elements={elements}
          actions={actions}
          showAvatar={showAvatar}
          showBorder={showBorder}
          key={i}
          indent={indent}
          isRunning={_isRunning}
          isLast={isLast}
          setRandom={setRandom}
          random={random}
        />
      );
    })}
  </>
);
}

